package csu.rjxyg.service.impl;

import java.util.List;

import csu.rjxyg.action.constant.ServiceConstant;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.dao.BaseDAO;
import csu.rjxyg.dao.BaseDaoAdapter;
import csu.rjxyg.factory.core.BeanFactory;
import csu.rjxyg.factory.core.DaoFactory;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.util.LogUtil;

public class UserService implements BaseUserService {
	private BeanFactory factory;
	private LogUtil log;
	
	
	
	public UserService(){
		factory = BeanFactory.getBeanFactory();
		log = UtilFactory.getLogUtil(getClass());
	}
	
	@Override
	public BaseObject login(String userId, String userPass) {
		// TODO Auto-generated method stub
		BaseObject bean;
		try{
			bean = (BaseObject) factory.getBean(BaseBean.BEAN_OBJECT);
			bean.set(UserConstant.USER_ID, userId);
			bean.set(UserConstant.USER_PASS, userPass);
			BaseDaoAdapter userDAO = DaoFactory.getDaoByColName(UserConstant.COLLECTION_NAME);
			BaseList temp = userDAO.getBeanInfoByExample(bean);
			if( temp.size() == 0 || temp.size() > 1 ){
				return null;
			}
			bean = temp.get(0);
		}catch(Exception e){
			bean = null;
			log.info("调用userDAO的login方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return bean;
		
	}

	@Override
	public BaseList getAllBeanInfo() {
		// TODO Auto-generated method stub
		BaseList list = null;
		try{
			String[] fieldName = {  UserConstant.USER_QX, UserConstant.USER_ID};
			int[] status = { 1, 1  };
			BaseDaoAdapter userDAO = DaoFactory.getDaoByColName(UserConstant.COLLECTION_NAME);	
			list =  userDAO.getAllBean(fieldName, status);
		}catch(Exception e){
			log.info("调用userDAO的getAllBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}

		return list;		
	}
	

	@Override
	public BaseList getAllPermissionInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean insertUserBean(BaseObject object) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter userDAO = DaoFactory.getDaoByColName(UserConstant.COLLECTION_NAME);	
			return userDAO.insertBean(object);
		}catch(Exception e){
			log.info("调用userDAO的getAllBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}

	@Override
	public boolean deleteUser(String userId) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter userDAO = DaoFactory.getDaoByColName(UserConstant.COLLECTION_NAME);	
			return userDAO.deleteBean(userId);
		}catch(Exception e){
			log.info("调用userDAO的deleteBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}

	@Override
	public boolean deleteUser(String[] userId) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter userDAO = DaoFactory.getDaoByColName(UserConstant.COLLECTION_NAME);	
			return userDAO.deleteBean(userId);
		}catch(Exception e){
			log.info("调用userDAO的deleteBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}

	@Override
	public boolean updateUserBean(String userId, BaseObject object) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter userDAO = DaoFactory.getDaoByColName(UserConstant.COLLECTION_NAME);
			return userDAO.updateBean(userId, object);
		}catch(Exception e){
			log.info("调用userDAO的updateBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}

	@Override
	public BaseObject getBeanInfoById(String beanId) {
		BaseObject bean = null;
		try{
			BaseDaoAdapter userDAO = DaoFactory.getDaoByColName(UserConstant.COLLECTION_NAME);
			bean =  userDAO.getBeanInfoById(beanId);
		}catch(Exception e){
			log.info("调用userDAO的getBeanInfoById方法发生错误,此时beanId为"+beanId);
			log.info(e.toString());
			e.printStackTrace();			
		}
		return bean;
	}
	
//	@Override
//	public String getBeanIdByName(String beanName){
//		String id = null;
//		BaseObject object = (BaseObject) factory.getBean(BaseBean.BEAN_OBJECT);
//		object.set(UserConstant.USER_NAME, beanName);
//		try{
//			BaseDaoAdapter userDAO = DaoFactory.getDaoByColName(UserConstant.COLLECTION_NAME);
//			List list = userDAO.getBeanInfoByExample(object);
//			if( list. )
//			object = (BaseObject) .get(0);
//			id = (String) object.get(UserConstant.USER_ID);
//		}catch(Exception e){
//			log.info("调用userDAO的getBeanInfoByExample方法发生错误,此时beanId为"+ id);
//			log.info(e.toString());
//			e.printStackTrace();			
//		}
//		return id;
//	}

}
